package com.lagou.es.controller;

import com.lagou.es.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class PositionController {
    @Autowired
    private PositionService  service;

    // 测试页面
    @GetMapping({"/","/index"})
    public   String  indexPage(){
        return  "index";
    }


    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    @ResponseBody
    public  List<Map<String,Object>>  searchPosition(@PathVariable("keyword") String  keyword,@PathVariable("pageNo")int pageNo,
                                                     @PathVariable("pageSize")int  pageSize)throws  IOException{
        List<Map<String,Object>> list = service.searchPos("positionName",keyword, pageNo, pageSize);
        if(list.size() < 5){
            list.addAll(service.searchPos("positionAdvantage","美女多 员工福利好",pageNo,pageSize)
                    .subList(0,5-list.size()));
        }
        return  list;
    }

    @RequestMapping("/importAll")
    @ResponseBody
    public   String  importAll(){
        try {
            service.importAll();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  "success";
    }
}
